package com.kuaiditu.assistant.api.kdy_api;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.kuaiditu.assistant.api.ParamsAnalog;
import com.kuaiditu.assistant.api.kdy_api.base.BaseSTRequest;
import com.kuaiditu.assistant.api.kdy_api.base.BaseSTResponse;
import com.kuaiditu.assistant.api.kdy_api.kdy_encode.KdyMD5Util;
import com.kuaiditu.assistant.entity.Operator;
import com.kuaiditu.assistant.module.retrofit.FastJsonConverterFactory;
import com.kuaiditu.assistant.module.retrofit.impl.STConverterAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by Eenie on 2017/8/11.
 * Email: eeniework@gmail.com
 * Des:
 */

public class STApi {
    private STService mKdyService;
    private SimpleDateFormat mSimpleDateFormat;
    private ParamsAnalog mParamsAnalog;
    private String onlyCode;

    public STApi(Context app, OkHttpClient client, ParamsAnalog paramsAnalog) {
        mParamsAnalog = paramsAnalog;

        mKdyService = new Retrofit.Builder()
                .baseUrl("http://dts.kuaidihelp.com")
                .client(client)
                .addConverterFactory(FastJsonConverterFactory.create(new STConverterAdapter()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(STService.class);

        onlyCode = KdyMD5Util.getOnlyCode(app);

        mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }



    public Observable<BaseSTResponse> addAcceptExpress(String express, Operator operator) {
        Map<String, Object> params = new HashMap<>();
        params.put("sname", "scan.to.e3.v2");
        params.put("appVersion", "111");
        params.put("wayBillType", "2");
        params.put("dev_id", onlyCode);
        params.put("dev_imei", mParamsAnalog.getDevice_imei());
        params.put("signPics", new Object());
        params.put("sendSms", "0");
        params.put("pname", "androids");

        List dataList = new ArrayList();

        Map<String, Object> data = new HashMap<>();


        Map<String, Object> location = new HashMap<>();

        location.put("latitude", mParamsAnalog.getDevice_lat());
        location.put("longitude", mParamsAnalog.getDevice_lng());

        data.put("waybillNo", express);
        data.put("scan_time", mSimpleDateFormat.format(System.currentTimeMillis()));
//        data.put("latitude", mParamsAnalog.getDevice_lat());
//        data.put("longitude", mParamsAnalog.getDevice_lng());
        data.put("operatorCode", operator.getSt_uid());
        data.put("weight", 0.5);
        data.put("resType", "1");
        data.put("location", location);
        dataList.add(data);
        params.put("wayBillDatas", dataList);

        return mKdyService.fetchData("session_id=" + operator.getSt_ssid(), BaseSTRequest.DEFAULT(params).build());
    }

    public Observable<BaseSTResponse> addSignExpress(String express, Operator operator) {
        Map<String, Object> params = new HashMap<>();
        params.put("sname", "scan.to.e3.v2");
        params.put("appVersion", "111");
        params.put("wayBillType", "3");
        params.put("dev_id", onlyCode);
        params.put("dev_imei", mParamsAnalog.getDevice_imei());
        params.put("signPics", new Object());
        params.put("sendSms", "0");
        params.put("pname", "androids");

        List dataList = new ArrayList();

        Map<String, Object> data = new HashMap<>();


        data.put("waybillNo", express);
        data.put("scan_time", mSimpleDateFormat.format(System.currentTimeMillis()));
        data.put("latitude", mParamsAnalog.getDevice_lat());
        data.put("longitude", mParamsAnalog.getDevice_lng());
        data.put("signType", "本人签收");

        dataList.add(data);
        params.put("wayBillDatas", dataList);

        return mKdyService.fetchData("session_id=" + operator.getSt_ssid(), BaseSTRequest.DEFAULT(params).build());
    }

    public Observable<ResponseBody> fetchCom(String express) {
        return null;
    }

    public Observable<ResponseBody> login(String account, String pwd) {
        long t = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        sb.append(t)
                .append("4accd1296e8f514627a411e4e2fbfc5f")
                .append("/v1/" + "Wduser/login".replace(".", "/"))
                .append("10002");
        FormBody.Builder builder = new FormBody.Builder();
        Map<String, Object> params = new HashMap<>();
        params.put("user_name", account);
        params.put("user_pwd", pwd);
        builder.add("app_id", "10002");
        builder.add("ts", String.valueOf(t));
        builder.add("sign", KdyMD5Util.toMD5(sb.toString()));
        builder.add("data", JSON.toJSONString(params));
        return mKdyService.login("", builder.build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }



}
